<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends MY_Controller{

    public function __construct(){
        parent::__construct();
        $this->load->helper('url');
    }
    /**
     * 修改密码
     * @param  string $day [description]
     * @return [type]      [description]
     */
    public function changepwd() {
        $data['status'] = 'normal';
        $this->load->view("login/edit.html", $data);
    }

    /**
     * 修改密码表单处理
     */
    public function changepwdform() {
        $org_password = ($this->input->post('org_password')) ? html_escape($this->input->post('org_password')) : "";
        $data['status'] = 'normal';
        if(empty($org_password) || md5($org_password) != $_SESSION['inet_pw']) {
            $data['error'] = "原密码输入错误";
            $this->load->view("login/edit.html", $data);
            return;
        }
        $pw = ($this->input->post('pw')) ? html_escape($this->input->post('pw')) : "";
        $repw = ($this->input->post('repw')) ? html_escape($this->input->post('repw')) : "";
        if(empty($pw) || $pw != $repw) {
            $data['org_password'] = $org_password;
            $data['error'] = "两次密码不一致";
            $this->load->view("login/edit.html", $data);
            return;
        }
        $this -> load -> model('user_model');

        if($this -> user_model -> edit_password($pw)) {
            $data['status'] = 'success';
            $this -> load -> view("login/edit.html", $data);
        } else {
            $this -> load -> view("login/edit.html", $data);
        }
    }


    //数据概览
    public function indexShowSum(){
        $sum = array();

        $last = time() - 7*60*60*24;
        $lastweekStr = getdate($last);
        $lastCurrday = date('Y-m-d', $last);
        $diffDay = $lastweekStr['wday'];
        $diffnum = getMondayDiffDay($diffDay);
        $lastMonday = getMonday($diffnum,$lastCurrday);

        //timeon
        $this -> load -> model('timeon_model');
        $enter_timeon = $this->timeon_model->getAllEnterprize();//echo $enter_timeon.'--timeon<br>';
        $devices_timeon = $this->timeon_model->AllDevices();
        $active_timeon_day = $this->timeon_model->yesterday_active();
        $active_timeon_week = $this->timeon_model->week_active($lastMonday); //echo 'time week active:'.$active_timeon_week;
        $active_timeon_mon = $this->timeon_model->month_active();

         //eqq
         $this -> load -> model('timeoneqq_model');
         $enter_eqq = $this->timeoneqq_model->getAllEnterprize();//echo $enter_eqq.'--eqq<br>';
         //month active
         $active_eqq_mon = $this->timeoneqq_model->month_active();
         //week active
         $active_eqq_week = $this->timeoneqq_model->week_active($lastMonday); //echo '<br>eaa week active:'.$active_eqq_week;
         //day active
         $active_eqq_day = $this->timeoneqq_model->yesterday_active();
         //devices
         $devices_eqq = $this->timeoneqq_model->AllDevices(); //echo $devices_eqq; exit;


         //cloud
         $this -> load -> model('cloud_model');
         $enter_cloud = $this->cloud_model->getAllEnterprize();// echo $enter_cloud.'--cloud<br>';
         $devices_cloud = $this->cloud_model->AllDevices();
         $active_cloud_day = $this->cloud_model->yesterday_active();
         $active_cloud_week = $this->cloud_model->week_active($lastMonday); //echo '<br>cloud week active:'.$active_cloud_week; exit;
         $active_cloud_mon = $this->cloud_model->month_active(); //echo $active_cloud_mon; exit;

         $sum['all_enterprize'] = $enter_eqq + $enter_timeon + $enter_cloud;
         $sum['all_device'] = $devices_timeon + $devices_eqq + $devices_cloud;
         $sum['active_day'] = $active_timeon_day + $active_eqq_day + $active_cloud_day;
         $sum['active_week'] = $active_timeon_week + $active_eqq_week + $active_cloud_week;
         $sum['active_mon'] = $active_timeon_mon + $active_eqq_mon + $active_cloud_mon;

        //print_r($sum); exit;
        echo json_encode($sum);
        exit;
    }

    //终端情况
    public function terminalInfo(){
        $this -> load -> model('timeon_model');
        $re = $this->timeon_model->getTerminal();
        echo json_encode($re);exit;

    }

    //企业排行、地域分部
    public function regionalInfo(){
        $this -> load -> model('timeon_model');
        $re = $this->timeon_model->getRegional();
        echo json_encode($re);exit;

    }



}
?>